<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/13
 * Time: 16:56
 */
//申明HTTP消息头的文档编码格式
header('content-type:text/html;charset=utf-8');
require './public_function.php';
//链接数据库
//dbInit();
$link = mysqli_connect('localhost','root','','itcast');
if(!$link) {
    die('链接数据库失败' . mysqli_error($link));
}
mysqli_query($link,'set names utf8');
$sql = 'select *from `emp_info`';
$result = mysqli_query($link,$sql);
//定义数组员工，用以保存员工信息
$emp_info =array();
//遍历结果集,获取每位员工的信息
while ($row = mysqli_fetch_assoc($result)){
    $emp_info[]=$row;
}

//$emp_info = fetchAll($link,$sql);
//初始化排序语句，用来组合排序的order子句
$fields = array('e_dept','data_of_entry');
$sql_order = '';
/*判断$_GET['order']是否存在，如果存在则将其赋值给$order；如果不存在则把空字符串
赋值给$order **/
$order = isset($_GET['order']) ? $_GET['order'] : '';
$sort = isset($_GET['sort']) ? $_GET['sort'] : '';
//判断$_GET['sort']是否存在并且值为'desc'
if($order){
    if($sort == 'desc'){
        //条件成立，组合order子句  order by 字段 desc
        $sql_order = "order by $order desc";
        //更新$sort为'asc'
        $sort = 'asc';
    }else{
        //条件不成立，组合order子句，order by 字段 asc
        $sql_order = "order by $order asc";
        //更新$sort为'desc'
        $sort = 'desc';
    }
}


//准备SQL语句
$sql = "select * from`emp_info` $sql_order";
//执行 SQL语句，获取结果集
$result = mysqli_query($link,$sql );
$res = mysqli_query($link,$sql);
$emp_info = array();
while ($row = mysqli_fetch_assoc($res)){
    $emp_info[] = $row;
}


$where = '';
//判断是否有搜索关键字传入
if(isset($_GET['keyword'])){
    $keyword = $_GET['keyword'];
    $keyword = mysqli_real_escape_string($link,$keyword);
    $where = "where e_name like '%$keyword%'";
}
$sql2 = "select * from`emp_info` $where";
$res2 = mysqli_query($link,$sql2);
$emp_info2 = array();
while ($row2= mysqli_fetch_assoc($res2)) {
    $emp_info2[] = $row2;
}
//$sql = 'select * from `emp_info`';
//$emp_info = fetchAll($link,$sql);

define('APP','itcase');
require './list_html.php';




